package com.pc.cloud.recommend.stat;

import com.pc.cloud.recommend.KeywordStat;
import com.pc.cloud.services.DocumentBuilder;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

public class StatDocumentBuilder implements DocumentBuilder {

	private List<KeywordStat> keywordStats;

	public StatDocumentBuilder(List<KeywordStat> searchStats) {
		this.keywordStats = searchStats;
	}

	public String getSearchType() {
		return "stat";
	}

	public List<Document> getDocs() throws Exception {
		List<Document> docs = new ArrayList<Document>();
		try {
			for (KeywordStat ss : keywordStats) {
				try {
					// 搜索无结果或者搜索次数小于2次，不建索引
					if (ss.getResult() == null
							|| ss.getResult().intValue() <= 0
							|| ss.getSearches().intValue() < 2) {
						continue;
					}

					Document doc = new Document();
					doc.add(new Field(StatSearchFields.Keyword.ID, UUID
							.randomUUID().toString(), Field.Store.YES,
							Field.Index.NOT_ANALYZED));
					doc.add(new Field(
							StatSearchFields.Keyword.KEYWROD_UNTOKENIZIED, ss
									.getKeyword(), Field.Store.NO,
							Field.Index.NOT_ANALYZED));

					doc.add(new Field(StatSearchFields.Keyword.SEACHES, String
							.valueOf(ss.getSearches()), Field.Store.YES,
							Field.Index.NOT_ANALYZED));

					doc.add(new Field(StatSearchFields.Keyword.RESULT, String
							.valueOf(ss.getResult()), Field.Store.YES,
							Field.Index.NOT_ANALYZED));

					try {
						doc.add(new Field(
								StatSearchFields.Keyword.STAT_DATE,
								StatSearchFields.dayFormat.format(ss.getStatDate()),
								Field.Store.YES, Field.Index.NOT_ANALYZED));
					} catch (Exception e) {
						e.printStackTrace();
					}

					docs.add(doc);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return docs;
	}

	public Collection<String> getKeys() throws Exception {
		List<String> keys = new ArrayList<String>();

		keys.add("123456654rtrgbvcd5511");

		/*
		 * for(KeywordStat ss : keywordStats) { keys.add(ss.getKeyword()); }
		 */return keys;
	}
}
